package com.osstem;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 클래스설명 : InsertAmount
 * @version : 2011. 11. 14.
 * @author : jw
 * @분류 : 
 * 설명 : 
 * ChangeSystem / package com.osstem;
 */

public class InsertAmount extends Activity implements OnClickListener, Runnable{
	private DbManager dbManager;	//dbManager 객체
	private ProgressDialog dialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.insertamount);
	    
	    //버튼 초기화
	    Button insert_amount = (Button)findViewById(R.id.insert_amount);
	    insert_amount.setOnClickListener(this);
	    
	    Button back = (Button)findViewById(R.id.back);
	    back.setOnClickListener(this);
	}
	
	
	public void dbInit(){
    	dbManager = new DbManager(this);
    }
	
	@Override
	public void onClick(View v) {
		
		Log.i("realTest", "v.getId() = " + v.getId());
		
		//등록버튼 클릭시
		if(v.getId() == R.id.insert_amount){
			Log.i("realTest", "regist");
			//insert 시작
			dialog = ProgressDialog.show(this, "","저장중입니다...", true);
			Thread thread = new Thread(this);
			thread.start();
		}
		//뒤로버튼 클릭시
		else if(v.getId() == R.id.back){
			Intent intent = new Intent(InsertAmount.this, ChangeSystemActivity.class);
			startActivity(intent);
			finish();
		}
	}
	
	
	
	/** 
	 * Method   : insert
	 * 최초작성일  : 2011. 11. 21. 
	 * 작성자 : jw
	 * 변경이력 :  
	 * Method 설명 : 잔액 입력 메서드 
	 */
	private void insert(){
		dbInit();
		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.amount);
		RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
		
		Intent param = getIntent();
		String sabun = param.getStringExtra("sabun");
		Log.i("realTest", "sabun = " + sabun);
		
		String amount = ((String)radioButton.getText()).replace(",", "").replace("원", "");
		//db 종료
		dbManager.insertAmount(sabun, amount);
	}
	
	
	/** 
	 * Method   : endActivity
	 * 최초작성일  : 2011. 11. 21. 
	 * 작성자 : jw
	 * 변경이력 :  
	 * Method 설명 : activity 종료 
	 */
	private void endActivity(){
		Intent intent = new Intent(InsertAmount.this, ChangeSystemActivity.class);
		startActivity(intent);
		finish();
	}
	
		
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if(keyCode == event.KEYCODE_BACK){
    		AlertDialog alert = new AlertDialog.Builder( this )
    		  .setIcon( R.drawable.icon )
    		  .setTitle( "종료" )
    		  .setMessage( "종료하시겠습니까?" )
    		  .setPositiveButton( "OK", new DialogInterface.OnClickListener(){
	    		   @Override
	    		   public void onClick(DialogInterface dialog, int which){
	    			   finish();
	    			   System.exit(0);
	    		   }
    		  }).setNegativeButton( "Cancel", new DialogInterface.OnClickListener(){
    			   public void onClick(DialogInterface dialog, int which){
    				   //아무동작 안한다.
    			   }
    		  }).show();  //end alert
    	}//end if
		return true;
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(dbManager != null){
			dbManager.closeReadalbeDb();
			dbManager.closeWritableDb();
		}
	}




	@Override
	public void run() {
		
		try {
			Thread.sleep(400);
			insert();
			dialog.dismiss();
			endActivity();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
